package com.vegetable.modules.service.inbound.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.vegetable.common.utils.Constant;
import com.vegetable.modules.entity.baseData.ProductSpec;
import com.vegetable.modules.entity.baseData.Products;
import com.vegetable.modules.entity.inbound.AgentDeliveryDetail;
import com.vegetable.modules.entity.inbound.AgentDeliveryDetailPack;
import com.vegetable.modules.service.baseData.IProductSpecService;
import com.vegetable.modules.service.inbound.IAgentDeliveryDetailPackService;
import com.vegetable.modules.service.inbound.IInboundStockService;
import com.vegetable.modules.service.stock.IStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 入库库存服务实现类
 */
@Slf4j
@Service
public class InboundStockServiceImpl implements IInboundStockService {
    
    @Resource
    private IStockService stockService;
    
    @Resource
    private IAgentDeliveryDetailPackService detailPackService;
    
    @Resource
    private IProductSpecService specService;

    @Override
    public IStockService getStockService() {
        return stockService;
    }

    @Override
    public void saveInStock(Date orderTime, BigDecimal inMoney, Products products, Long specsId, String specsName,
                         int inNumber, BigDecimal inWeight, int storageType, Long warehouseId, String warehouseName,
                         String agentName, String packageCost, String docNo, Long orderId, Long detailId, BigDecimal unitGoodsPrice) throws Exception {
        log.info("进入saveInStock方法，detailId: {}", detailId);
        try {
            log.info("准备调用stockService.inStock方法，detailId: {}", detailId);
            if (products == null){
                log.info("未找到商品，commodityId: {}", detailId);
                return;
            }
            log.info("找到商品，commodityId: {}, commodityName: {}", products.getId(), products.getCommonName());
            
            log.info("获取包装信息完成，packs数量: {}", packageCost != null ? JSONArray.parseArray(packageCost).size() : 0);
            
            log.info("获取规格信息完成，specsName: {}", specsName);
            
            log.info("计算参数，inNumber: {}, inWeight: {}", inNumber, inWeight);
            
            log.info("条件判断，inNumber != 0: {}, !NumberUtil.equals(inWeight, BigDecimal.ZERO): {}", 
                inNumber != 0, !NumberUtil.equals(inWeight, BigDecimal.ZERO));
                
            if(inNumber != 0 || !NumberUtil.equals(inWeight, BigDecimal.ZERO)){
                log.info("调用stockService.inStock方法，参数: orderTime={}, inMoney={}, productId={}, specsId={}, specsName={}, inNumber={}, inWeight={}, detailId={}", 
                    orderTime, inMoney, products.getId(), specsId, specsName, inNumber, inWeight, detailId);
                stockService.inStock(orderTime, inMoney, products, specsId, specsName, inNumber, inWeight, storageType, warehouseId, warehouseName, agentName, packageCost, docNo, orderId, detailId, unitGoodsPrice);
                log.info("stockService.inStock方法调用完成，detailId: {}", detailId);
            }else{
                log.info("条件不满足，未调用stockService.inStock方法，detailId: {}", detailId);
            }
            log.info("saveInStock方法执行完成，detailId: {}", detailId);
        } catch (Exception e) {
            log.error("saveInStock方法执行过程中发生错误，detailId: {}", detailId, e);
            throw e;
        }
    }
}